<?php

namespace Home\Controller;

use Think\Controller;

require 'Aliyun/aliyun.php';

use \Aliyun\OSS\OSSClient;

class AliyunController extends HomeController{
// oss-internal.aliyuncs.com 
// oss-cn-qingdao-internal.aliyuncs.com
	public $client;
	public $bucket;
	public $url;
	public $w_hz_host = '.oss-cn-hangzhou.aliyuncs.com/';
	public $host = '.oss-internal.aliyuncs.com/';
	public $qd_host = '.oss-cn-qingdao-internal.aliyuncs.com/';
	public function __construct(){
		parent::__construct();

		$this->client = OSSClient::factory(array(
		    'AccessKeyId' => '',
		    'AccessKeySecret' => '',
		));
		// echo '<pre>';

		$this->bucket = '';
		$this->url = 'http://'.$this->bucket.$this->w_hz_host;
	}

	public function index(){
		$this->display();
	}

	public function create(){
		var_dump($this->client);
		$res = $this->client->createBucket(array(
    		'Bucket' => 'missqltest',
		));
		var_dump($res);
	}

	public function upload(){
// Array ( [file] => Array ( [name] => iphone.png [type] => image/png [tmp_name] => /tmp/phpbonBXQ [error] => 0 [size] => 45671 ) )

		$file_name = $_FILES['file']['name'];
		$file_type = $_FILES['file']['type'];
		$tmp_name  = $_FILES['file']['tmp_name'];
		$size 	   = $_FILES['file']['size'];
		$allow_type = array(
			'image/jpg',
			'image/jpeg',
			'image/png',
			'image/gif',
			'audio/ogg',
			'audio/mp3',
			'video/mp4',
			'text/plain',
		);
		// echo $file_type;
		if (!in_array($file_type, $allow_type)){
			exit('error type');
		}
		if ( in_array( $file_type , array('image/jpg','image/jpeg','image/png','image/gif') )){
			$save_path = 'images';
		}elseif ( in_array( $file_type , array('audio/ogg','audio/mp3') )){
			$save_path = 'audio';
		}elseif ( in_array( $file_type , array('video/mp4') )){
			$save_path = 'video';
		}else{
			$save_path = 'other';
		}

		$content = '';
		$length  = 0;

		$fp = fopen( $tmp_name ,'r');
        if($fp){
            $f = fstat($fp);
            $length = $f['size'];
            while(!feof($fp)){
                $content .= fgets($fp,8192);
            }
        }
        fclose($fp);

        $url = $this->uploadPut( $save_path.'/'.$file_name , $content , $length );

        echo $url;

        // 使用分块上传可能会快点 读一点上传一点
	}

	public function uploadPut( $key , $content , $length ){
		$res = $this->client->putObject(array(
		    'Bucket' => $this->bucket,
		    'Key' => $key,
		    'Content' => $content,
		    'ContentLength' => $length,
		));
		$file_url = $this->url.$key;
		return $file_url;
	}

	

	public function put(){
		$path = 'Public/audio/';
		$file = 'ED6563.ogg';
		$file_path = $path.$file;
		$fp = fopen( $file_path , 'r');
		$size = filesize( $file_path );
		$key = $file;
		$res = $this->client->putObject(array(
		    'Bucket' => $this->bucket,
		    'Key' => $key,
		    'Content' => $fp,
		    'ContentLength' => $size,
		));
		$file_url = $this->url.$key;
		echo "<audio src='$file_url'></audio>";
		// echo '<a href="'.$file_url.'">go</a>';
		// var_dump($res);
	}

	public function multiput(){
		// 需要支持断点上传。
		// 上传超过100MB大小的文件。
		// 网络条件较差，和OSS的服务器之间的链接经常断开。
		// 需要流式地上传文件。
		// 上传文件之前，无法确定上传文件的大小。

	}

}// -END

